//
//  DPPictureSelectorToolBar.m
//  DailyPhoto
//
//  Created by 黄凯展 on 16/7/26.
//  Copyright © 2016年 ShengCheng. All rights reserved.
//

#import "DPPictureSelectorToolBar.h"
#import "Masonry.h"
#import "BMLineView.h"

@interface DPPictureSelectorToolBar ()
/***  取消按钮*/
@property (nonatomic, weak) UIButton *cancelButton;
/***  确定按钮*/
@property (nonatomic, weak) UIButton *confirmButton;
/***  数字*/
@property (nonatomic, weak) UILabel *countLabel;
@end

@implementation DPPictureSelectorToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.maxCount = 9;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    [self setupCancelButton];
    
    [self setupConfirmButton];
    
    [self setupCountLabel];
    
    [self setupLineView];
}

- (void)setupLineView
{
    BMLineView *lineView = [BMLineView defaultLineView];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(1);
    }];
}

- (void)setupCancelButton
{
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitleColor:[UIColor colorWithRed:80.0/255 green:60.0/255 blue:40.0/255 alpha:1.0] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:14 * DailyPhoto_FACTOR];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelButton];
    self.cancelButton = cancelButton;
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.width.mas_equalTo(50);
        make.left.mas_equalTo(6);
    }];
}

- (void)setupConfirmButton
{
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitleColor:[UIColor colorWithRed:80.0/255 green:60.0/255 blue:40.0/255 alpha:1.0] forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14 * DailyPhoto_FACTOR];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmButton];
    self.confirmButton = confirmButton;
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.width.mas_equalTo(50);
        make.right.mas_equalTo(-6);
    }];
}

- (void)setupCountLabel
{
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.textColor = [UIColor whiteColor];
    countLabel.font = [UIFont systemFontOfSize:SCREEN_WIDTH/375.0*(12)];
    countLabel.numberOfLines = 1;
    countLabel.backgroundColor = Color_RGB(243, 75, 42);
    countLabel.layer.cornerRadius = SCREEN_WIDTH/375.0*(7);
    countLabel.layer.masksToBounds = YES;
    countLabel.textAlignment = NSTextAlignmentCenter;
    countLabel.text = [NSString stringWithFormat:@"%zd/%zd",self.count,self.maxCount];
    [self addSubview:countLabel];
    self.countLabel = countLabel;
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(DPResize(9));
        make.right.mas_equalTo(DPResize(-3));
        make.height.mas_equalTo(DPResize(14));
        make.width.mas_equalTo(DPResize(24));
    }];
}

#pragma mark - 属性

- (void)setCount:(NSInteger)count
{
    _count = count;
    if (count >= 0) {
        self.countLabel.text = [NSString stringWithFormat:@"%zd/%zd",self.count,self.maxCount];
    }
}

- (void)setMaxCount:(NSInteger)maxCount
{
    _maxCount = maxCount;
    if (maxCount) {
        self.countLabel.text = [NSString stringWithFormat:@"%zd/%zd",self.count,self.maxCount];
    }
}

#pragma mark - 事件方法
- (void)cancelButtonDidClicked:(UIButton *)button
{
    if (self.cancelButtonClickedBlock) {
        self.cancelButtonClickedBlock(button,nil);
    }
}

- (void)confirmButtonDidClicked:(UIButton *)button
{
    if (self.confirmButtonClickedBlock) {
        self.confirmButtonClickedBlock(button,@(self.count));
    }
}
@end
